<template>
  <div style="height: 100%;background-color:white ;">
    <el-row>
      <el-col :span="12">
        <el-button plain size="small" type="success" @click="syncSelected">批量推送</el-button>
        <el-button plain size="small" type="success" @click="syncAll">全量推送</el-button>
      </el-col>
      <el-col :span="12">
        <div>
          <div style="float: right">
            <div style="display: inline-block; color: red;font-weight: bolder;">请选择推送平台:</div>
            <div style="display: inline-block">
              <el-input v-model="releasedFlowChart.name" placeholder="请输入名称" @keyup.enter.native="advanceConfirm">
                <el-select slot="prepend" v-model="releasedFlowChart.selectedPlatform" collapse-tags
                           placeholder="请选择推送平台"
                           style="width: 180px;" @change="selectItem">
                  <el-option
                    v-for="platform in platforms"
                    :key="platform.id"
                    :label="platform.name"
                    :value="platform.id">
                  </el-option>
                </el-select>
                <i slot="prefix" class="el-input__icon el-icon-search"></i>
                <el-button slot="append" :icon="appendIcon" v-on:click="switchAppend"></el-button>
              </el-input>
            </div>
          </div>
          <div v-if="showAdvance"
               id="advance"
               style="padding: 10px; float: right;">
            <div>
              <div style="margin-top: 10px; display: flex;">
                <div class="advancedSearchLabel">归属项目</div>
                <div style="width: 217px">
                  <el-cascader v-model="releasedFlowChart.belongingItems"
                               :options="itemOptions"
                               :props="cascadeProps"
                               :show-all-levels="false"
                               clearable
                               placeholder="归属项目"
                               size="small"
                               @change="handleChange">
                  </el-cascader>
                </div>
              </div>
            </div>
            <div>
              <div style="margin-top: 10px; display: flex;">
                <div class="advancedSearchLabel">科学分类</div>
                <div style="width: 217px">
                  <el-cascader v-model="releasedFlowChart.scienceItems"
                               :options="scienceItemOptions"
                               :props="cascadeProps"
                               :show-all-levels="false"
                               clearable
                               placeholder="科学分类"
                               size="small"
                               @change="scienceHandleChange"></el-cascader>
                </div>
              </div>
            </div>
            <div style="margin-top: 10px; display: flex;">
              <div class="advancedSearchLabel">状态</div>
              <div style="display: inline-block; width: 217px">
                <el-select v-model="releasedFlowChart.syncStatus" placeholder="状态" size="small">
                  <el-option
                    v-for="item in CONST.SYNC_STATUS"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                  </el-option>
                </el-select>
              </div>
            </div>
            <div style="margin-top: 30px; right: 10px;">
              <el-row>
                <el-col :offset="6" :span="6">
                  <div style="margin-left: 90px;height: 32px; width: 88px;">
                    <el-button size="small" v-on:click="advanceCancel">取 消
                    </el-button>
                  </div>
                </el-col>
                <el-col :offset="2" :span="2">
                  <div style="margin-left: 30px;height: 32px; width: 88px;">
                    <el-button size="small" v-on:click="reset">重置</el-button>
                  </div>
                </el-col>
                <el-col :offset="2" :span="2">
                  <div style="margin-left: 30px;height: 32px; width: 88px;">
                    <el-button size="small" type="success" v-on:click="advanceConfirm">确 定</el-button>
                  </div>
                </el-col>
              </el-row>
            </div>
          </div>
        </div>
      </el-col>
    </el-row>
    <el-row style="margin-top: 10px;">
      <el-col :span="24">
        <el-table :data="tableData" :height="tableHeight" border
                  highlight-current-row @selection-change="handleSelectionChange">
          <el-table-column
            v-model="multipleSelection"
            type="selection"
            width="55">
          </el-table-column>
          <el-table-column label="名称"
                           min-width="180"
                           prop="flowName"></el-table-column>
          <el-table-column label="归属项目"
                           min-width="100"
                           prop="belongingItemName"></el-table-column>
          <el-table-column label="科学分类"
                           min-width="100"
                           prop="scienceTagName"></el-table-column>
          <el-table-column label="版本号"
                           min-width="100"
                           prop="version"></el-table-column>
          <el-table-column label="版本号"
                           min-width="60"
                           prop="version"></el-table-column>
          <el-table-column label="创建人"
                           min-width="80"
                           prop="createdBy">
            <template slot-scope="scope">
              {{ getNameByUserName(scope.row.createdBy) }}
            </template>
          </el-table-column>
          <el-table-column
            label="推送时间"
            min-width="140"
            prop="operatedTime"></el-table-column>
          <el-table-column label="推送状态" min-width="120" prop="syncStatus">
            <template slot-scope="scope">
              <span v-if="scope.row.syncStatus === 0" style="color: red">推送失败</span>
              <span v-else-if="scope.row.syncStatus === 1" style="color: red">消费失败</span>
              <span v-else-if="scope.row.syncStatus === 3">推送成功</span>
              <span v-else style="color: #FF8C00">待推送</span>
            </template>
          </el-table-column>
          <el-table-column label="操作"
                           min-width="150">
            <template slot-scope="scope">
              <span><el-link type="primary" @click.prevent="syncSingle(scope.row.id)">推送</el-link></span>
              <span><el-link type="primary" @click.prevent="mobile(scope.row.id)">详情</el-link></span>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>
    <el-row>
      <el-pagination
        v-if="multiplePages"
        :page-size="pageSize"
        :page-sizes="[5, 10, 15, 20, 50, 100]"
        :total="totalNum"
        background
        layout="total, sizes, prev, pager, next, jumper"
        style="text-align: right;margin-top: 10px"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"></el-pagination>
    </el-row>
  </div>
</template>

<script>

export default {
  name: "ReleasedFlowChart",
  components: {},
  props: ["detailStatus"],
  data() {
    return {
      tableData: [],
      tableHeight: '',
      multipleSelection: [],
      totalNum: 0,
      multiplePages: true,
      appendIcon: "el-icon-caret-bottom",
      showAdvance: false,
      itemOptions: [],
      scienceItemOptions: [],
      belongingItems: "",
      scienceItems: "",
      radioStatus: '',
      cascadeProps: {multiple: false},
      releaseEnable: '',
      editEnable: 'none',
      checkAllEnable: '',
      delEnable: '',
      selectedFlowChart: {},
      platforms: [],
      pageSize: 10,
      releasedFlowChart: {
        currentPage: 1, // 第几页
        pageSize: 10
      }
    }
  },
  methods: {
    //复选框选中事件
    handleSelectionChange(selection) {
      this.multipleSelection = [];
      this.multipleSelection = selection;
    },
    advanceCancel: function () {
      this.showAdvance = false;
    },
    advanceConfirm: function () {
      this.releasedFlowChart.currentPage = 1;
      this.showAdvance = false;
      this.getTableData();
    },
    switchAppend: function () {
      if (this.showAdvance) {
        this.showAdvance = false;
        this.appendIcon = "el-icon-caret-bottom";
      } else {
        this.showAdvance = true;
        this.appendIcon = "el-icon-caret-top";
      }
    },
    searchEnterFun(e) {
      const keyCode = window.event ? e.keyCode : e.which;
      if (keyCode === 13) {
        this.releasedFlowChart.currentPage = 1;
        this.getTableData();
      }
    },
    reset: function () {
      this.releasedFlowChart.flowName = "";
      this.releasedFlowChart.belongingItems = "";
      this.releasedFlowChart.scienceItems = "";
      this.releasedFlowChart.syncStatus = "";
    },
    handleCurrentChange: function (val) {
      this.releasedFlowChart.currentPage = val;
      this.getTableData();
    },
    handleChange(value) {
      this.belongingItems = value;
    },
    scienceHandleChange(value) {
      this.scienceItems = value;
    },
    dateChange(val) {
      this.createdTime = val;
      if (val == null) {
        this.createdTime = ''
      }
    },
    mobile(id) {
      let routeUrl = this.$router.resolve({
        path: "/flowchart/mobileViewFlowChart",
        query: {id: id, status: 'published'},
      });
      window.open(routeUrl.href, "_blank");
    },
    getTableData: function () {
      if (UTIL.isEmpty(this.releasedFlowChart.selectedPlatform)) {
        return;
      }
      this.axios.post('/pmph/action/flowChartAction/selectSyncList', this.releasedFlowChart).then((response) => {
        this.noData = response.data.total <= 0;
        this.tableData = response.data.records;
        this.totalNum = response.data.total;
      })
    },
    handleSizeChange(value) {
      this.releasedFlowChart.pageSize = value
      this.getTableData()
    },
    syncSingle(id) {
      this.releasedFlowChart.syncIds = id;
      this.syncToPlatform();
    },
    syncSelected() {
      if (this.multipleSelection.length <= 0) {
        this.$message.warning("请选择推送的流程图");
        return;
      }
      this.releasedFlowChart.syncIds = this.multipleSelection.map(item => item.id).join(",");
      this.syncToPlatform();
    },
    syncAll() {
      this.syncToPlatform();
    },
    syncToPlatform() {
      this.axios.post('/pmph/action/flowChartAction/syncToPlatform', this.releasedFlowChart).then(() => {
        this.$message.success('推送任务提交成功, 请稍后查看推送状态');
      })
    },
    selectItem(val) {
      this.releasedFlowChart.currentPage = 1;
      this.releasedFlowChart.selectedPlatform = val;
      this.getTableData();
    },
    getNameByUserName(userName) {
      return this.$store.state.userList.filter(item => item.userName === userName)[0].name;
    },
  },
  created() {
    window.addEventListener("visibilitychange", (e) => {
      if (document.visibilityState !== "hidden") {
        this.getTableData();
      }
    });
    if (this.detailStatus === 'true') {
      this.radioStatus = 'published'
      this.editEnable = 'none'
      this.checkAllEnable = 'none'
      this.releaseEnable = 'none'
    } else {
      this.radioStatus = 'unpublished'
    }
    this.tableHeight = CONST.WORKSPACE_HEIGHT - 160;
    // 获取分类数据
    this.axios
      .get("/pmph/action/flowChartAction/selectBelongingItemsData")
      .then((response) => {
        this.itemOptions = response.data;
      })

    // 获取分类数据
    this.axios
      .get("/pmph/action/flowChartAction/selectScienceData")
      .then((response) => {
        this.scienceItemOptions = response.data;
      })

    //获取关联平台
    this.axios.get("/pmph/action/knowledgeLib/selectAllPlatforms", {
      params: {
        type: CONST.SYSTEM.FLOWCHART
      }
    }).then((res) => {
      this.platforms = res.data.platforms;
      if (UTIL.isNotEmpty(this.platforms) && this.platforms.length > 0) {
        this.releasedFlowChart.selectedPlatform = this.platforms[0].id;
        this.getTableData()
      }
    })
  }
}
</script>

<style scoped>

</style>
